<?php
class sms_model_sms_data extends Model {
	private $_reltable = '';
	public function __construct(){
		parent::__construct();
		$this->table = 'app_sms';
		$this->_reltable = 'app_sms_session';
	}
	
	public function add($arguments){
		$arguments = filter_params($arguments, 'uid', 'username', 'reciever_count', 'message_count', 'type', 'target');
		trim_params($arguments);
		if(!$arguments['target']) $arguments['target'] = "";
		$arguments['create_time'] = time();
		$flag = $this->db->insert($this->table, $arguments);
		return $flag;
	}
	
	public function select($sms_id){
		$sql = "select * from ".$this->table." where id=&id";
		$params = array('id'=>$sms_id);
		return $this->db->fetchRow($sql, $params);
	}
	
	public function updateCount($sms_id){
		$sql = "update ".$this->table." set message_count=message_count+1 where id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->query($sql, $params);
	}
	/*
	public function updateSmsCount($uid,$type,$cnt = 1){
		if(!is_numeric($uid)) return false;
		if(is_array($uid) && !empty($uid)){
			$uid = implode(',',$uid);
		}
		$set = ' SET ';
		!$cnt && $cnt = 0;
		switch ($type){
			case sms_model_message::MSG_SMS_CNT :
				$set.=($cnt>0)?("asc_sms =asc_sms+$cnt"):('asc_sms =0') ;
				break;
			case sms_model_message::MSG_SYS_SMS_CNT :
				$set.=($cnt>0) ?("asc_sysann+$cnt"):('asc_sysann=0,asc_friends=0,asc_gift=0,asc_buff=0');
				break;
			case sms_model_message::MSG_MESSAGE_CNT :
				$set.=($cnt>0)?(" asc_message=asc_message+$cnt"):' asc_message=0';
				break;
			case sms_model_message::MSG_COMMENT_CNT :
				$set.=($cnt>0)?("asc_comment+$cnt"):' asc_comment=0';
				break;
			case sms_model_message::MSG_SYS_FRIENDS :
				$set.=($cnt>0)? (" asc_friends= asc_friends+$cnt,asc_sysann=asc_sysann+$cnt"):('asc_sysann=asc_sysann-asc_friends,asc_friends=0 ');
				break;
			case sms_model_message::MSG_SYS_GIFT  :
				$set.=($cnt>0)?(" asc_gift=asc_gift+$cnt,asc_sysann=asc_sysann+$cnt"):(' asc_sysann=asc_sysann-asc_gift,asc_gift=0 ');
				break;
			case sms_model_message::MSG_SYS_BUFF  :
				$set.=($cnt>0)?(" asc_buff=asc_buff+$cnt,asc_sysann=asc_sysann+$cnt"):(' asc_sysann=asc_sysann-asc_buff,asc_buff=0 ');
				break;
			default:
				return false;
				break;
		}
		$sql = "UPDATE app_sms_count  $set where uid IN ($uid)";		
		return $this->db->query($sql);
	}*/
	
	public function updateSmsCount($uid,$type,$cnt = 1){
		if(!is_numeric($uid)) return false;
		if(is_array($uid) && !empty($uid)){
			$uid = implode(',',$uid);
		}
		$sql = '';
		!$cnt && $cnt = 0;
		switch ($type){
			case sms_model_message::MSG_SMS_CNT :
				$sql.='INSERT INTO app_sms_count(uid,asc_sms) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_sms=asc_sms+'.$cnt ):(' asc_sms=0' );
				break;
			case sms_model_message::MSG_SYS_SMS_CNT :
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_sysann=asc_sysann+'.$cnt ):(' asc_sysann=0,asc_friends=0,asc_gift=0,asc_buff=0 ' );
				break;
			case sms_model_message::MSG_MESSAGE_CNT :
				$sql.='INSERT INTO app_sms_count(uid,asc_message) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_message=asc_message+'.$cnt ):(' asc_message=0' );
				break;
			case sms_model_message::MSG_COMMENT_CNT :
				$sql.='INSERT INTO app_sms_count(uid,asc_comment) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(' asc_comment=asc_comment+'.$cnt ):(' asc_comment=0' );
				break;
			case sms_model_message::MSG_SYS_FRIENDS :
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_friends= asc_friends+$cnt,asc_sysann=asc_sysann+$cnt"):(' asc_sysann=asc_sysann-asc_friends,asc_friends=0' );
				break;
			case sms_model_message::MSG_SYS_GIFT  :
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_gift= asc_gift+$cnt,asc_sysann=asc_sysann+$cnt"):(' asc_sysann=asc_sysann-asc_gift,asc_gift=0' );
				break;
			case sms_model_message::MSG_SYS_BUFF  :
				$sql.='INSERT INTO app_sms_count(uid,asc_sysann) VALUES ('.$uid.',1) on DUPLICATE KEY UPDATE ';
				$sql.=($cnt>0)?(" asc_buff=asc_buff+$cnt,asc_sysann=asc_sysann+$cnt "):(' asc_sysann=asc_sysann-asc_buff,asc_buff=0 ' );
				break;
			default:
				return false;
				break;
		}
		if($sql) return $this->db->query($sql);
		return false;
	}
	public function cleanSmsCount($uid,$type){
		return self::updateSmsCount($uid,$type,0);
	}
	
	public function setReaded($uid,$type){
		if(!is_numeric($uid)) return false;
		switch ($type){
			case sms_model_message::MSG_SYS_SMS_CNT:
			case sms_model_message::MSG_SYS_FRIENDS:
			case sms_model_message::MSG_SYS_GIFT :
			case sms_model_message::MSG_SYS_BUFF:
				$type = 0;
				break;
			default:
				$type = 1;
				break;
		}
		$sql = 'UPDATE app_sms_reciever a SET a.unread = 0 WHERE a.uid=&uid AND a.type=&type';
		$para = array('uid' => $uid,'type' =>$type);
		return $this->db->query($sql,$para);
	}
	
	public function queryUnReadSms($uid){
		if(!is_numeric($uid)) return false;
		$sql = "SELECT asc_sms as smsnum,asc_sysann as sysnum,asc_comment as comnum,asc_message as msgnum,asc_friends as friendsnum,asc_buff as buffnum,asc_gift as giftnum FROM app_sms_count WHERE uid=&uid ";
		$params = array('uid' =>$uid);
		if($rst = $this->db->fetchAll($sql, $params)){
			return $rst[0];
		}
		return false;
	}
	
	public function updateTarget($id, $target){
		$sql = "update ".$this->table." set target=&target where id=&id" ;
		$target['id'] = $id;
		$params = array(
			'id'=>$id,
			'target'=>serialize($target)
		);
		$this->db->query($sql, $params);
	}
	
	public function rollback($sms_id){
		$sql = "delete from ".$this->table." where id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->query($sql, $params);
	}
}